<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>ClojureScript学习笔记一（Hello World与REPL） - (power up)</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="Kevin Jiang" /><meta name="description" content="学习一门编程语言就像新出来的婴儿一样，一个裸体可以看清全部。学习编程语言也是一样的，一开始不可以使用任何一个工具。 ClojureScript" /><meta name="keywords" content="KevinJiang, AI大模型, AI落地, 全栈工程师, Java, Spring Boot" />






<meta name="generator" content="Hugo 0.84.4 with theme even" />


<link rel="canonical" href="http://kevinjiang.info/post/clojure/clojurescript%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B8%80hello-world%E4%B8%8Erepl/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.78f8f17bab244b9ee62ad16480c9584d5fc2db06ae20681d1ca225cefd80767c.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="ClojureScript学习笔记一（Hello World与REPL）" />
<meta property="og:description" content="学习一门编程语言就像新出来的婴儿一样，一个裸体可以看清全部。学习编程语言也是一样的，一开始不可以使用任何一个工具。 ClojureScript" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://kevinjiang.info/post/clojure/clojurescript%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B8%80hello-world%E4%B8%8Erepl/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2016-08-22T17:40:54+00:00" />
<meta property="article:modified_time" content="2016-08-22T17:40:54+00:00" />

<meta itemprop="name" content="ClojureScript学习笔记一（Hello World与REPL）">
<meta itemprop="description" content="学习一门编程语言就像新出来的婴儿一样，一个裸体可以看清全部。学习编程语言也是一样的，一开始不可以使用任何一个工具。 ClojureScript"><meta itemprop="datePublished" content="2016-08-22T17:40:54+00:00" />
<meta itemprop="dateModified" content="2016-08-22T17:40:54+00:00" />
<meta itemprop="wordCount" content="1735">
<meta itemprop="keywords" content="ClojureScript,ClojureScript REPL," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="ClojureScript学习笔记一（Hello World与REPL）"/>
<meta name="twitter:description" content="学习一门编程语言就像新出来的婴儿一样，一个裸体可以看清全部。学习编程语言也是一样的，一开始不可以使用任何一个工具。 ClojureScript"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">(Power up)</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/resume/">
        <li class="mobile-menu-item">简历</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">(Power up)</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/resume/">简历</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">ClojureScript学习笔记一（Hello World与REPL）</h1>

      <div class="post-meta">
        <span class="post-time"> 2016-08-22 </span>
        
          <span class="more-meta"> 约 1735 字 </span>
          <span class="more-meta"> 预计阅读 4 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents"></nav>
  </div>
</div>
    <div class="post-content">
      <p>学习一门编程语言就像新出来的婴儿一样，一个裸体可以看清全部。学习编程语言也是一样的，一开始不可以使用任何一个工具。</p>
<p>ClojureScript继承了Clojure简单和富有表现力的语法，编译后的代码可以运行在浏览器和Node.js上面。</p>
<p>编写hello world程序。新建一个目录<code>cljs_first_project</code>，然后还需要建立存放源代码的目录。下载<a href="https://github.com/clojure/clojurescript/releases/latest">编译器</a>{:target=&quot;_blank&quot;}，目录结构如下：</p>
<pre><code>cljs_first_project
|- build.clj
|- cljs.jar
|- src
    |- cljs_first_project
        |- core.cljs
</code></pre>
<p>在core.cljs写入如下内容：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">(ns cljs-first-project.core)
(js/alert &#34;Hello world!&#34;)
</code></pre></td></tr></table>
</div>
</div><p>这段代码定义一个命名空间，然后是一个javascript里面的alert弹出框。另外还需要写一个clojure文件来对源代码进行编译，在根目录下新建一个build.clj文件，内容如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">(require &#39;cljs.build.api)
(cljs.build.api/build &#34;src&#34; {:output-to &#34;out/main.js&#34;})
</code></pre></td></tr></table>
</div>
</div><p>包含<code>cljs.build.api</code>命名空间，然后调整<code>build</code>函数，此函数接受两个参数，第一个是ClojureScript源代码目录，第二个是是一个map数据结果，这里指定了输出编译后的js的路径。现在使用前面下载的编译器进行编译：</p>
<p>Windows下编译</p>
<pre><code>java -cp &quot;cljs.jar;src&quot; clojure.main build.clj
</code></pre>
<p>*nix下编译</p>
<pre><code>java -cp cljs.jar:src clojure.main build.clj
</code></pre>
<p>编译完成后会在out目录生成main.js，同时还有其他一些目录。要运行main.js还需要一个greet.html，内容如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-html" data-lang="html"><span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span>
  <span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">script</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text/javascript&#34;</span> <span class="na">src</span><span class="o">=</span><span class="s">&#34;out/goog/base.js&#34;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">script</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text/javascript&#34;</span> <span class="na">src</span><span class="o">=</span><span class="s">&#34;out/main.js&#34;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">script</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text/javascript&#34;</span><span class="p">&gt;</span>
      <span class="nx">goog</span><span class="p">.</span><span class="nx">require</span><span class="p">(</span><span class="s2">&#34;cljs_first_project.core&#34;</span><span class="p">)</span>
    <span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
  <span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>用浏览器打开greet.html就可以看到alert框了。第一个ClojureScript编写编译完成！</p>
<p>再来一点高级功能，把<code>goog/base.js</code>依赖从html去掉，把<code>goog.require(&quot;cljs_first_project&quot;)</code>也从html里面去掉，只需要包含main.js就可以让程序跑起来。只需要对build.clj修改一下，内容修改如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">(require &#39;cljs.build.api)
(cljs.build.api/build
  &#34;src&#34;
  {:main &#39;cljs-first-project.core
  :output-to &#34;out/main.js&#34;})
</code></pre></td></tr></table>
</div>
</div><p>greet.html的内容如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-html" data-lang="html"><span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span>
  <span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">script</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text/javascript&#34;</span> <span class="na">src</span><span class="o">=</span><span class="s">&#34;out/main.js&#34;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
  <span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>打开greet.html，效果跟之前一样。</p>
<p>另外一个高级功能是当编辑源代码的时候不需要手动多次编译，另外在根目录下再写一个watch.clj文件，内容如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">(require &#39;cljs.build.api)
(cljs.build.api/watch
  &#34;src&#34;
  {:main &#39;cljs-first-project.core
  :output-to &#34;out/main.js&#34;})
</code></pre></td></tr></table>
</div>
</div><p>只是把调用build的地方改成调用watch函数即可。运行<code>java -cp &quot;cljs.jar;src&quot; clojure.main watch.clj</code>（*nix运行<code>java -cp cljs.jar:src clojure.main watch.clj</code>），此时若修改源文件core.cljs，编译器会自动检测到文件的修改，然后自动进行编译，自动编译后在浏览器刷新即可看到效果。</p>
<p>既然是lisp系，怎么可以少得了REPL(Read-Eval-Print-Loop)。REPL可以让我们直接把代码片段推送到浏览器运行，只需要在REPL里面写上代码，然后立马可以在浏览器看到效果，这个Live Codiing的基础。</p>
<p>同样的要想运行repl也需要clojure的支持，在根目录下新建一个repl.clj，内容如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">(require &#39;cljs.repl)
(require &#39;cljs.build.api)
(require &#39;cljs.repl.browser)
    
(cljs.build.api/build
  &#34;src&#34;
  {:main &#39;cljs-first-project.core
   :output-to &#34;out/main.js&#34;
   :verbose true})
    
(cljs.repl/repl
  (cljs.repl.browser/repl-env)
  :watch &#34;src&#34;
  :output-dir &#34;out&#34;)
</code></pre></td></tr></table>
</div>
</div><p>调整的build函数还是没有变，后面添加一个<code>cljs.repl/repl</code>函数调用。·cljs.repl.browser/repl-env·这个函数定义了一些默认配置，比如打开一个9000端口作为http的服务端口。<code>cljs.repl/repl</code>函数作了下面几件事情：</p>
<ol>
<li>监听源代码的修改</li>
<li>编译修改的源代码</li>
<li>监听9000端口提供http服务</li>
<li>打开一个prompt等待输入</li>
</ol>
<p>修改core.cljs内容如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">(ns cljs-first-project.core
  (:require [clojure.browser.repl :as repl]))
    
(defonce conn
  (repl/connect &#34;http://localhost:9000/repl&#34;))
    
(js/alert &#34;Hello world!&#34;)
</code></pre></td></tr></table>
</div>
</div><p>可以看到这里初始化了一个connection，连接到9000端口的/repl，这里使用defonce是只初始化一次，之后的每次修改都不再连接服务端的REPL。</p>
<p>Ok再来运行repl.clj</p>
<pre><code>java -cp &quot;cljs.jar;src&quot; clojure.main repl.clj
</code></pre>
<p>*unix</p>
<pre><code>java -cp cljs.jar:src clojure.main repl.clj
</code></pre>
<p>第一编译需要等待一小会，因为需要编译源代码启动http服务，最后会输出等待浏览器连接的提示：</p>
<pre><code>Waiting for browser to connect ...
</code></pre>
<p>现在用浏览器打开<code>http://localhost:9000/greet.html</code>，浏览器会显示一个Hello world!的弹出框。这时候命令行会出现一个输入的提示符，如下：</p>
<pre><code>Watch compilation log available at: out\watch.log
To quit, type: :cljs/quit
cljs.user=&gt; 
</code></pre>
<p>输入<code>(js/alert &quot;Hello World From REPL!&quot;)</code>回车，浏览器就是看到这段代码的效果。</p>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">Kevin Jiang</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2016-08-22
        
    </span>
  </p>
  
  <p class="copyright-item">
    <span class="item-title">许可协议</span>
    <span class="item-content"><a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank">CC BY-NC-ND 4.0</a></span>
  </p>
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/wechat_pay_1242x1242.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/alipay_600x600.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/clojurescript/">ClojureScript</a>
          <a href="/tags/clojurescript-repl/">ClojureScript REPL</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/java/scala%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">Scala学习笔记之正则表达式</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/webpack%E4%B9%8Bhello-world/">
            <span class="next-text nav-default">Webpack之Hello World</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:wenlin1988@126.com" class="iconfont icon-email" title="email"></a>
      <a href="https://www.linkedin.com/in/%E6%96%87%E6%9E%97-%E8%92%8B-0a3204126/" class="iconfont icon-linkedin" title="linkedin"></a>
      <a href="https://github.com/kevindragon" class="iconfont icon-github" title="github"></a>
  <a href="http://kevinjiang.info/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 -
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy;
    2015 -
    2023<span class="heart"><i class="iconfont icon-heart"></i></span><span>Kevin Jiang</span>
    <a href="https://beian.miit.gov.cn/" target="_blank">湘ICP备2022022745号</a>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        tags: 'ams',
        }
    };
  </script>
  <script type="text/javascript" async src="/lib/mathjax/es5/tex-mml-chtml.js"></script>

<script id="baidu_analytics">
  var _hmt = _hmt || [];
  (function() {
    if (window.location.hostname === 'localhost') return;
    var hm = document.createElement("script"); hm.async = true;
    hm.src = "https://hm.baidu.com/hm.js?b73ff6d4afc4af9e582d8a5dc068bab9";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>






</body>
</html>
